import request from "./request"

// 微信小程序支付
export function wxPay(url = '', data = {}) {
	return new Promise((resolve, reject) => {
		uni.showLoading({
			mask: true
		})
		request.post(url, data).then(res => {
			// console.log(res)
			let payData = {}
			if (res.code == 200) {
				if (res.data.orderNo) {
					uni.showToast({
						title: '支付成功',
						icon: 'success'
					})
					resolve(result)
					return
				}
				if (!res.data.data.timeStamp) {
					uni.showToast({
						title: '购买失败！',
						icon: 'none'
					})
					return
				}
				const data = res.data.data
				Object.assign(payData, {
					appid: data.appId,
					timeStamp: data.timeStamp,
					nonceStr: data.nonceStr,
					package: data.package,
					signType: data.signType,
					paySign: data.paySign,
				})
				uni.requestPayment({
					provider: 'wxpay',
					...payData,
					success: (res2) => {
						uni.showToast({
							title: '支付成功',
							icon: 'success'
						})
						resolve(res2)
					},
					fail: (err) => {
						/* uni.showToast({
							title: '支付失败',
							icon: 'none'
						}) */
						reject(err)
					},
					complete: () => {
						uni.hideLoading()
					}
				});
			} else {
				uni.showToast({
					title: res.message,
					icon: 'none'
				})
			}
		})
	})
}